home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 11555 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.7 KB  |  110 lines

  1. Newsgroups: comp.lang.c
  2. Path: new-news.sprintlink.net!eskimo!news
  3. From: mag@eskimo.com (mAg)
  4. Subject: Re: Variant Records in C ... Is there a way ?
  5. X-Nntp-Posting-Host: tia1.eskimo.com
  6. Message-ID: <DotGHL.4wr@eskimo.com>
  7. Sender: news@eskimo.com (News User Id)
  8. Organization: *.*
  9. X-Newsreader: WinVN 0.93.10
  10. References: <Pine.OSF.3.91.960319170252.9783B-100000@alfa.ist.utl.pt>
  11. Date: Mon, 25 Mar 1996 09:17:44 GMT
  12.  
  13. In article <Pine.OSF.3.91.960319170252.9783B-100000@alfa.ist.utl.pt> (Tue, 19 Mar 1996 
  14. 17:12:18 +0000), l41471@alfa.ist.utl.pt says :
  15. >
  16. >
  17. >        I would like to know if there is any  way of implementing in C a 
  18. >structure that if a determined argument (or info or depending on the 
  19. >value of a particular field of a structure) could ignore or acknoledge a 
  20. >certain field of the structure ... 
  21. >
  22. >        Example :
  23. >                struct bla {
  24. >                        int field1;
  25. >                        int field2;
  26. >                        int field3;
  27. >                        } 
  28. >        And having a struct looking like this one, consider field3 if 
  29. >field1=1, or ignore it (by not placing it in the data structure thus 
  30. >saving memory ...) 
  31. >
  32. >        Any help would be highly  apreciated ... (even if it's just to 
  33. >say that there isn't a way ... it would make me stop searching for it ;))
  34. >
  35. >
  36. >        Regards from :
  37. >                        Nuno Miguel Almeida Silva                          
  38. >                        l41471@alfa.ist.utl.pt
  39. >                        http://alfa.ist.utl.pt/~l41471
  40. >Instituto Superior Tecnico - Lisbon - Portugal
  41. >There's a major genocide going on in East Timor 
  42.  
  43. This is commonly done. Here is a crude example
  44. union _data_union_
  45.   {
  46.   char ch;
  47.   int i;
  48.   long l;
  49.   float f;
  50.   double d;
  51.   void *p;
  52. / * and whatever else that you want */
  53. ...
  54. ...
  55.   };
  56.  
  57. typedef struct _variant_data_
  58. {
  59. int iDataType;
  60. union _data_union_ u;
  61. } VariantData;
  62.  
  63. #define VARIANTDATAchar 1
  64. #define VARIANTDATAint 2
  65. #define VARIANTDATAlong 3
  66. #define VARIANTDATAfloat 4
  67. #define VARIANTDATAdouble 5
  68. #define VARIANTDATApointer 6
  69.  
  70. And the Access Functions ...
  71.  
  72.  
  73. SetVariantInt(VariantData *pvd, int i)
  74. {
  75. pvd->u.i = i;
  76. pvd->iDataType = VARIANTDATAint;
  77. }
  78.  
  79.  
  80. And the function with a pointer to VariantData as an argument
  81.  
  82. DoSomethingWithVariant(VariantData *pvd)
  83. {
  84. switch(pvd->iDataType)
  85.   {
  86.   case  VARIANTDATAchar :
  87. /* do something with   pvd->u.ch */
  88. ...
  89. ...
  90.  
  91.  
  92.  
  93.  
  94.  
  95.   }
  96.  
  97. }
  98.  
  99.  
  100. hope that gives the idea...
  101. -- 
  102. /* --------------------------------------------------------
  103.                       MAG@ESKIMO.COM
  104. http://www.eskimo.com/~mag/index.html
  105. ***********************************************************
  106. To understand recursion one must first understand recursion
  107. ***********************************************************
  108. -------------------------------------------------------- */
  109.  
  110.